package com.sky.controller.user;

import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.*;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("userOrderController")
@RequestMapping("/user/order")
@Api(tags = "用户端-订单相关接口")
@Slf4j
public class OrderController {

    @Autowired
    private OrderSubmitService orderSubmitService;
    @Autowired
    private OrderHistoryService orderHistoryService;
    @Autowired
    private OrderCancelService orderCancelService;
    @Autowired
    private OrderAgainService orderAgainService;
    @Autowired
    private UserOrderReminderService userOrderReminderService;

    /**
     * 用户下单
     * @param ordersSubmitDTO 订单数据
     * @return 结果
     */
    @PostMapping("/submit")
    @ApiOperation("用户下单")
    public Result<OrderSubmitVO> submitOrder(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        log.info("用户提交订单");
        OrderSubmitVO orderSubmitVO = orderSubmitService.submitOrder(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }

    // TODO
    // 订单支付

    /**
     * 历史订单查询
     * @param page
     * @param pageSize
     * @param status   订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消
     * @return
     */
    @GetMapping("/historyOrders")
    @ApiOperation("历史订单查询")
    public Result<PageResult> page(int page, int pageSize, Integer status) {
        PageResult pageResult = orderHistoryService.pageQuery(page, pageSize, status);
        return Result.success(pageResult);
    }

    /**
     * 查询历史订单详情
     * @param id 订单id
     * @return 订单详情
     */
    @GetMapping("/orderDetail/{id}")
    @ApiOperation("查询订单详情")
    public Result<OrderVO> historyOrdersDetail(@PathVariable Long id){
        log.info("查询订单详情");
        OrderVO orderVO = orderHistoryService.historyOrdersDetail(id);
        return Result.success(orderVO);
    }

    /**
     * 取消订单
     * @param id 订单id
     * @return
     */
    @PutMapping("/cancel/{id}")
    @ApiOperation("取消订单")
    public Result<String> cancelOrder(@PathVariable Long id){
        log.info("取消订单");
        orderCancelService.cancelOrder(id);
        return Result.success("取消订单成功");
    }

    @PostMapping("/repetition/{id}")
    @ApiOperation("再来一单")
    public Result<String> againOrder(@PathVariable Long id) {
        log.info("再来一单");
        orderAgainService.againOrder(id);
        return Result.success("再来一单成功");
    }

    @GetMapping("/reminder/{id}")
    @ApiOperation("催单")
    public Result<String> reminder(@PathVariable Long id) {
        log.info("催单");
        userOrderReminderService.reminder(id);
        return Result.success("催单成功");
    }
}
